package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * 使用反射机制调用方法
 */
public class ReflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Person p =new Person();
        p.sayHello();

        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入类名:");
        String className =scanner.next();
        System.out.println("请输入方法名:");
        String methodName = scanner.next();

        Class cls =Class.forName(className);
        Object o = cls.newInstance();//Person p =new Person();
        //获取要调用的方法
        //仅传入方法名时,是获取该无参方法
        Method method = cls.getMethod(methodName);//表示的Person的成员方法sayHello
        method.invoke(o);//o.sayHello


    }
}
